﻿using Microsoft.Phone.Info;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace SMARTMobile
{
    public static partial class SMReloadService
    {
        public static event Action OnStateChanged;
        public static event Action<string, Exception> OnError;

        public enum ServiceState
        {
            Connecting,
            Loading,
            Loaded,
            Agreed,
        }

        public enum ApplicationState
        {
            Eula,
            NewApp,
            Sms,
            Shopping,
            Login,
            LoginSuccess,
        }

        #region Fields

        // AppSettings keys
        private const string APP_STATE_KEY = "application_state";
        private const string DEVICE_ID_KEY = "uid_device_id";

        // parameter names
        private const string CONTENT_ID = "contentId";
        private const string PARTNER_ID = "partnerId";
        private const string LOCATION_CODE = "locationCode";
        private const string PRODUCT_NUMBER = "productNumber";
        private const string SOURCE_CODE = "sourceCode";
        private const string POST_DECISTION = "postDecision";

        private static ServiceState _state;
        private static ApplicationState _applicationState;

        private static string _sessionId;
        private static string _deviceId;
        private static readonly Uri _baseUri;
        private static readonly string _userAgent;
        private static readonly string _deviceName;

        public static bool IsLoaded { get; private set; }
        public static string ErrorMessage { get; private set; }

        #endregion

        static SMReloadService()
        {
            State = ServiceState.Connecting;
            _deviceName = DeviceStatus.DeviceName;
            _baseUri = new Uri(SMConfiguration.Current.BaseUri);
            _userAgent = string.Format(SMConfiguration.Current.UserAgentFormat, _deviceName);
            Debug.WriteLine(_userAgent);
        }

        public static string DeviceId
        {
            get
            {
                if (!string.IsNullOrWhiteSpace(_deviceId)) return _deviceId;

                if (IsolatedStorageSettings.ApplicationSettings.Contains(DEVICE_ID_KEY))
                {
                    _deviceId = IsolatedStorageSettings.ApplicationSettings[DEVICE_ID_KEY] as string;
                    return _deviceId;
                }
                return _deviceId;
            }
            set
            {
                _deviceId = value;
                if (!IsolatedStorageSettings.ApplicationSettings.Contains(DEVICE_ID_KEY))
                {
                    IsolatedStorageSettings.ApplicationSettings.Add(DEVICE_ID_KEY, value);
                }
                else
                {
                    IsolatedStorageSettings.ApplicationSettings[DEVICE_ID_KEY] = value;
                }
                IsolatedStorageSettings.ApplicationSettings.Save();
            }
        }

        public static ServiceState State
        {
            get { return _state; }
            set
            {
                _state = value;
                if (null != OnStateChanged)
                    Deployment.Current.Dispatcher.BeginInvoke(() => OnStateChanged());
            }
        }

        public static ApplicationState AppState
        {
            get
            {
                _applicationState = ApplicationState.Eula;
                if (IsolatedStorageSettings.ApplicationSettings.Contains(APP_STATE_KEY))
                {
                    _applicationState = (ApplicationState)IsolatedStorageSettings.ApplicationSettings[APP_STATE_KEY];
                }
                return _applicationState;
            }
            set
            {
                _applicationState = value;
                if (!IsolatedStorageSettings.ApplicationSettings.Contains(APP_STATE_KEY))
                {
                    IsolatedStorageSettings.ApplicationSettings.Add(APP_STATE_KEY, _applicationState);
                }
                else
                {
                    IsolatedStorageSettings.ApplicationSettings[APP_STATE_KEY] = _applicationState;
                }
                IsolatedStorageSettings.ApplicationSettings.Save();
            }
        }

        private static void FireOnError(Exception exception)
        {
            if (null != OnError)
                Deployment.Current.Dispatcher.BeginInvoke(() => OnError(ErrorMessage, exception));
        }
    }
}
